require('../../libs');
const router = require('koa-router')();
const hwconfig = require('../consts/hwapi');

router.prefix('/hwcloud')

router.get('/metric', async (ctx, next) => {
  let date = new Date();
  let nextDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);

  let servers = hwconfig.getAllServers();

  let groups = [];

  servers.forEach(c => {
    let group = groups.find(g => g.projectId == c.projectId);

    if (group) {
      group.items.push(c);
    } else {
      groups.push({
        projectId: c.projectId,
        describle: c.describle,
        items: [c]
      });
    }
  });

  await ctx.render('hwcloud/metric', {
    title: '服务器设备监控',
    metrics: hwconfig.metrics,
    groupedServers: groups,
    fromDefault: `${date.format('yyyy-MM-dd')} 00:00`,
    toDefault: `${nextDate.format('yyyy-MM-dd')} 00:00`
  });
});

module.exports = router;